layui.config({
    base : "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index','form','jquery', 'table'],function(){
    var form = layui.form,
        table=layui.table,
        admin  = layui.admin;
       table.render({
        elem: '#agency_quota_table'
        ,url: '/agency_quota/queryData'
        ,method:"post"
        ,height: 500
        ,limit:50							//显示数目
        ,limits:[50,100]
        ,cols: [[
            {field:'levelId',width:200, title: '等级ID',"align":"left"}
            ,{field:'levelName',width:200, title: '等级名称',"align":"left"}
            ,{field:'quantity',width:200, title: '数量',"align":"left"}
            ,{field:'OPT', width:100, title: '操作',"align":"left",templet:function (d) {
                    return     '<a href="#" class="layui-btn layui-btn-xs" lay-event="quota_buy_btn" >购买</a>';
                }}
        ]]
        ,request:{
            pageName: 'pageNo' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where:{}
        ,id: 'agency_quota_table'
        ,page: false
    });

    table.on('tool(agency_quota_table)', function(obj){

        var table_event = obj.event;

        var data = obj.data;

        console.debug(data);

        if(table_event=="quota_buy_btn"){

            admin.popup({
                title: '名额购买'
                ,shade: 0
                ,anim: -1
                ,offset:"20px"
                ,area: ['80%', '500px']
                ,id: 'layadmin-layer-skin-modify'
                ,skin: 'layui-anim layui-anim-upbit'
                ,content:$("#buy_quota_win")
                ,end:function () {
                    table.reload('agency_quota_table', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                        }
                    });
                }
            });

            form.val('buy_quota_form', {
                "levelId":data.levelId,
                "levelName":data.levelName
            });

        }

    });

    form.on('submit(buyQuotaBtn)',function (data) {

        $.ajax({
            url:"/agency_quota/buyQuota",
            data:data.field,
            type:"post",
            success:function (result) {
                if (result.code == 1){
                    layer.msg("提交成功",{
                        offset: '32%'
                        ,icon: 1
                        ,time: 3000
                        ,btn: '知道了'
                        ,btnAlign:"c"
                        ,shade:0.4
                        ,yes:function () {
                            table.reload('agency_quota_table', {
                            });
                            layer.closeAll();
                        }
                    })
                }else{
                    layer.msg(result.message,{
                        offset: '32%'
                        ,icon: 2
                        ,time: 3000
                    },function () {
                        layer.closeAll();
                    })
                }

            }
        });
        return false;
    });


})

